package oop.controller.action.history;

import org.apache.commons.lang.StringUtils;

import oop.controller.action.DefaultAction;
import oop.data.History;
import oop.data.Test;
import oop.data.TestVersion;
import oop.db.dao.HistoryDAO;
import oop.db.dao.TestVersionDAO;
import oop.taglib.UtilFunctions;

public class ViewAction extends DefaultAction {

	@Override
	public void performImpl() throws Exception {
		try {
			long id = Long.parseLong(request.getParameter("id"));
			History history = HistoryDAO.fetchById(id).get(0);
			TestVersion version = TestVersionDAO.fetchById(history
					.getVersionId());
			Test test = version.getTest();

			String verCode = version.getCode();
			if (StringUtils.isEmpty(verCode)) {
				verCode = "";
			} else {
				verCode = " phiên bản " + verCode;
			}
			title("Đề " + test.getName() + verCode + " do "
					+ getUser().getFullname() + " làm vào "
					+ UtilFunctions.formatDateTime(history.getTakenDate()));
			request.setAttribute("history", history);
			request.setAttribute("version", version);
			request.setAttribute("test", test);
		} catch (NumberFormatException ex) {
			request.setAttribute("message", "ID không hợp lệ");
		} catch (IndexOutOfBoundsException ex) {
			request.setAttribute("message",
					"Không tìm thấy lịch sử có ID đã cho");
		}
	}

}
